'use strict';

/**
 * @param {Egg.Application} app - egg application
 */
module.exports = app => {
  const { router, controller } = app;
  // 中间件引入
  // const filter = app.middlewares.filter();

  // 角色信息
  const user = app.role.can('user'); // 注册用户
  const admin = app.role.can('admin'); // 管理员

  router.get('/admin/login', controller.admin.login.loginPage);
  router.post('/admin/login', controller.admin.login.login);
  router.get('/admin/logout', controller.admin.login.logout);
  // 后台部分
  router.get('/admin', admin, controller.admin.index.home);
  router.get('/admin/index', admin, controller.admin.index.index);

  // banner
  router.get('/admin/banner/list', admin, controller.admin.banner.list);
  router.get(
    '/admin/banner/addOrEdit',
    admin,
    controller.admin.banner.addOrEditJson
  );
  router.get('/admin/banner/add', admin, controller.admin.banner.addJson);
  router.get('/admin/banner/edit', admin, controller.admin.banner.editJson);
  router.get('/admin/banner/delete', admin, controller.admin.banner.deleteJson);

  // 文章相关
  router.get('/admin/article/list', admin, controller.admin.article.list);
  router.get('/admin/article/new', admin, controller.admin.article.new);
  router.get('/admin/article/edit', admin, controller.admin.article.edit);
  // json
  router.get(
    '/admin/article/delete',
    admin,
    controller.admin.article.deleteJson
  );
  router.get(
    '/admin/article/togglePublish',
    admin,
    controller.admin.article.togglePublishJson
  );
  router.post(
    '/admin/article/addOrEdit',
    admin,
    controller.admin.article.addOrEditJson
  );

  // 分类相关
  router.get(
    '/admin/articleType/list',
    admin,
    controller.admin.articleType.list
  );
  router.get(
    '/admin/articleType/add',
    admin,
    controller.admin.articleType.addJson
  );
  router.get(
    '/admin/articleType/addOrEdit',
    admin,
    controller.admin.articleType.addOrEditJson
  );
  router.get(
    '/admin/articleType/delete',
    admin,
    controller.admin.articleType.deleteJson
  );

  // 资源文件
  router.get('/admin/resource/list', admin, controller.admin.resource.list);
  router.get('/admin/resource/select', admin, controller.admin.resource.select);
  router.get('/admin/resource/detail', admin, controller.admin.resource.detail);
  router.get('/admin/resource/edit', admin, controller.admin.resource.editJson);
  router.get(
    '/admin/resource/clear',
    admin,
    controller.admin.resource.clearJson
  );
  router.get('/admin//admin/pdfview', controller.admin.resource.viewFile);

  // pdf文档解析
  router.get(
    '/admin/resource/parseFdp',
    admin,
    controller.admin.resource.parseFdp
  );

  // 资源文件夹
  router.get('/admin/folder/add', admin, controller.admin.folder.addJson);
  router.get('/admin/folder/edit', admin, controller.admin.folder.editJson);
  router.get('/admin/folder/clear', admin, controller.admin.folder.clearJson);

  // 日志
  router.get(
    '/admin/operation/list',
    admin,
    controller.admin.operationLog.list
  );

  // 用户
  router.get('/admin/user/list', admin, controller.admin.user.list);
  router.post(
    '/admin/user/editUserType',
    admin,
    controller.admin.user.editUserTypeJson
  );

  // 上传
  router.post('/upload', controller.upload.uploadJson);
  router.get('/admin/upload', controller.upload.uploadPage);

  /** 前台部分 *********************************/
  router.get('/', user, controller.front.index.index);

  // 登录
  router.get('/login', controller.front.login.loginPage);
  router.post('/login', controller.front.login.login);
  router.get('/logout', user, controller.front.login.logout);

  // 文章
  router.get('/article/list', user, controller.front.article.list);
  router.get('/article/detail', user, controller.front.article.detail);
  router.get('/article/search', user, controller.front.article.search);
  router.get('/api/article/search', user, controller.front.article.searchJson);
  // 文章浏览记录
  router.get(
    '/api/article/setArticleLog',
    user,
    controller.front.operationLog.setArticleLogJson
  );

  // pdf文档浏览
  router.get('/pdf/view', user, controller.front.resource.viewFile);
  router.get('/pdf/download', user, controller.front.resource.downloadFile);

  //全部
  router.get('/react/*', controller.react.index.index);
};
